$(function () {
    tchpterFun(); //目录展开与折叠
    lazyCourseImage();
    lazyStudentImage();
    var courseType = $("#courseType").val();
    var courseId = $('#courseId').val();

    QRCodeInp();
    validationStatus();
    function QRCodeInp(){
        var mobilePath = $('#mobilePath').val();
        var courseType = $('#courseType').val();
        var courseId = $('#courseId').val();
        if (courseType == 'LIVE'){
            link = mobilePath + '/mobile/course/liveInfo?courseId='+courseId + '&isFromCode';
        }else{
            link = mobilePath + '/mobile/course/info?courseId='+courseId + '&isFromCode';
        }
        var qrcode = $('#QRCodeInp').qrcode({render: "canvas", height: 140, width: 140, correctLevel: 2, text: link}).find('canvas').hide();
        var str = '<img src='+ qrcode[0].toDataURL()+ ' width=140 height=140 style=width:140px;height:140px; />'
        $("#QRCodeInp").attr('data-content', str)
    }

    $('body').on('click', 'aside.c-banner-cover-video', function () { // 免费试听
        if(courseType=='PACKAGE'){//套餐
            goPlayCourse(courseId,0, 0);
        }else{//普通课程
            goPlayCourse(0,courseId, 0);
        }
    }).on('click', 'h4[data-tab]', function () { //详情/目录/评论切换
        toggleCourseTag($(this));
    }).on('click', '.play-catalog-box', function () {//点击播放
        openPlayCourse(this,toPlayCourse)
    }).on('click', '.free-bm-box', function () {//免费报名
        var _this = $(this);
        _this.attr('disabled', 'disabled');
        var courseId = _this.data('course-id');
        var payType = _this.data('pay-type');
        var classesUrl = $("#classesUrl").val();
        //生成免费报名记录
        genFreeOrder(payType, courseId + '-COURSE-1', function () {
            _this.removeClass('free-bm-box');
            _this.addClass('play-btn');
            _this.find('span').text('立即学习');
            _this.removeClass("btn-danger");
            _this.addClass("btn-success");
            if (classesUrl != ""){
                layer.open({
                    type: 1,
                    title: '友情提示',
                    content: "免费报名成功,快去加入班级吧!",
                    btn: ['立即加入', '我再想想 '],
                    area: 'auto',
                    btnAlign: 'c',
                    shade: [0.3, '#000000'],
                    skin: 'lay_ppComment__wrap',
                    yes: function (index) {
                        window.location =  classesUrl;
                        layer.close(index);
                    },
                    btn2: function (index) {
                        layer.close(index);
                    }
                });
            }
        });
        _this.removeAttr('disabled');
    }).on('click','#pay-course',function () {//立即报名
        var type = $(this).find('span').data('type');
        if(type==1){//购买
            var shareKey = getParameter("shareKey");
            if(shareKey!=null && $.trim(shareKey) !=''){
                window.location.href = baseloaction +'/web/uc/order/initPayOrder?orderType=COMMON&shopData='+courseId+'-COURSE-1-'+shareKey;
            }else{
                window.location.href = baseloaction +'/web/uc/order/initPayOrder?orderType=COMMON&shopData='+courseId+'-COURSE-1';
            }
        }
        if(type==2){//开通会员
            window.location.href = baseloaction +'/web/uc/member/queryMemberList';
        }
    }).on('click','.askQuestions',function(){
        window.location.href = baseloaction +'/web/teacher/info?teacherId='+$(this).data("teacher-id");
    }).on('click','.a-s-c-d',function () {//添加购物车
        var shopId = $(this).data('s-id');
        var shopType = $(this).data('type');
        addShopCart(shopId,shopType,1);
    }).on('click','#favorites-course',function () {//收藏，取消收藏课程
        var _this = $(this);
        //收藏课程
        saveCourseFavorites(courseId,function (data) {
            var status = data.status;
            if(status =='add'){//添加收藏
                _this.addClass('c-primary').removeClass('c-999');
                _this.attr('title','已收藏');
                _this.html('<i class="fa fa-star-o mr5"></i>已收藏');
            }else{//取消收藏
                _this.removeClass('c-primary').addClass('c-999');
                _this.attr('title','收藏');
                _this.html('<i class="fa fa-star-o mr5"></i>收藏');
            }
        });
    }).on('click',"#online-consultation",function () {
        var content = $(this).data('course');
        window.open(content,'newwindow', 'toolbar=no,scro.llbars=yes,location=no,resizable=no,top=200,left=300,width=800,height=600');
    }).on('click', '.play-btn', function () {//点击播放 立即学习
        openPlayCourse(this,goPlayCourse);
    }).on('click','#show-receive-btn',function () {//点击领取显示
        var courseId = $("#courseId").val();
        getCourseCouponList(courseId)
    });

    /**
     * 课程详情 目录 评论切换
     */
    function toggleCourseTag(obj) {
        obj.addClass('active').siblings().removeClass('active');
        var tag = obj.data('tab');
        $('article[data-tab=' + tag + ']').removeClass('hide').siblings().addClass('hide');
        if(tag=='comments'){
            listComment(1); // 课程评价
        }
    }

    //加载课程可以使用的活动
    setTimeout(function () {
        var price = $("#price").val();
        //获取活动
        courseInfoActivity(price);
    },1000);
});